import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/draw_under_line.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 营销活动(可报名活动)
///
class MarketCanActivity extends StatefulWidget {
  const MarketCanActivity({Key? key}) : super(key: key);

  @override
  State<MarketCanActivity> createState() => _MarketCanActivityState();
}

class _MarketCanActivityState extends State<MarketCanActivity> {
  late ScrollController scrollController;

  bool isLoading = false;

  bool noMore = false;

  TextStyle get defaultStyle {
    return StylesUtils.customTextStyle(
        color: ColorsUtils.colorFb8, fontSize: Sizes.default13, lineHeight: 1.5);
  }

  TextStyle get style {
    return StylesUtils.customTextStyle(color: ColorsUtils.colorFb8, fontSize: Sizes.default15);
  }

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController()
      ..addListener(() {
        if (!isLoading &&
            !noMore &&
            scrollController.position.pixels >= scrollController.position.maxScrollExtent) {
          setState(() {
            isLoading = true;
            loadMoreData();
          });
        }
      });
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('可报名活动',
          actions: ToolsUtils.actions('报名记录', pressed: () {
            Get.toNamed(RouteGet.marketActivityRecord);
          })),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.all(Sizes.default20),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
              child: ListBody(
                children: [
                  DrawUnderLine(
                    color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                    child: Container(
                      padding: const EdgeInsets.only(
                          bottom: Sizes.default20,
                          top: Sizes.default10,
                          left: Sizes.default15,
                          right: Sizes.default15),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                'No.12',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 5,
                            child: Container(
                              padding: const EdgeInsets.only(left: Sizes.default10),
                              child: Column(
                                children: [
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '满500P减50P',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动细则：满500P减50P（减免部分，平台承担10P，商家承担40P）',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '用户类型：所有用户',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '使用方式：移动端',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '活动日期：2021-11-01 至 2021-11-30',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.only(top: Sizes.default10),
                                    child: Text(
                                      '报名日期：2021-11-01 至 2021-11-30',
                                      style: defaultStyle,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: Sizes.default60,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RouteGet.marketActivityInfo, arguments: true);
                            },
                            child: Text('活动详情', style: style)),
                        TextButton(onPressed: addActivity, child: Text('立即报名', style: style)),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(
                  left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
              child: ListBody(
                children: [
                  DrawUnderLine(
                    color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                    child: Container(
                      padding: const EdgeInsets.only(
                          bottom: Sizes.default20,
                          top: Sizes.default10,
                          left: Sizes.default15,
                          right: Sizes.default15),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                'No.11',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 5,
                            child: Container(
                              padding: const EdgeInsets.only(left: Sizes.default10),
                              child: Column(
                                children: [
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '免基础配送费',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动细则：满500P减50P（减免部分，平台承担10P，商家承担40P）',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '用户类型：所有用户',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '使用方式：移动端',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '活动日期：2021-11-01 至 2021-11-30',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.only(top: Sizes.default10),
                                    child: Text(
                                      '报名日期：2021-11-01 至 2021-11-30',
                                      style: defaultStyle,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: Sizes.default60,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextButton(onPressed: () {}, child: Text('活动详情', style: style)),
                        TextButton(onPressed: () {}, child: Text('立即报名', style: style)),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(
                  left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
              child: ListBody(
                children: [
                  DrawUnderLine(
                    color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                    child: Container(
                      padding: const EdgeInsets.only(
                          bottom: Sizes.default20,
                          top: Sizes.default10,
                          left: Sizes.default15,
                          right: Sizes.default15),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                'No.10',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 5,
                            child: Container(
                              padding: const EdgeInsets.only(left: Sizes.default10),
                              child: Column(
                                children: [
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '五折钜惠商家',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动细则：外卖订单五折优惠（不含团购订单、到店自取订单）',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '店铺要求：限早餐品类',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '用户类型：新注册用户',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '使用方式：移动端',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动日期：3个月',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '报名日期：长期',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.only(top: Sizes.default10),
                                    child: Text(
                                      '报名费用：￥5.00',
                                      style: defaultStyle,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: Sizes.default60,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RouteGet.marketActivityInfo, arguments: false);
                            },
                            child: Text('活动详情', style: style)),
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RouteGet.marketActivityPay);
                            },
                            child: Text('立即报名', style: style)),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(
                  left: Sizes.default20, right: Sizes.default20, bottom: Sizes.default20),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
              child: ListBody(
                children: [
                  DrawUnderLine(
                    color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                    child: Container(
                      padding: const EdgeInsets.only(
                          bottom: Sizes.default20,
                          top: Sizes.default10,
                          left: Sizes.default15,
                          right: Sizes.default15),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                'No.09',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color797),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 5,
                            child: Container(
                              padding: const EdgeInsets.only(left: Sizes.default10),
                              child: Column(
                                children: [
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '新店八折特惠',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default16, color: ColorsUtils.colorFf6),
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动细则：外卖订单五折优惠（不含团购订单、到店自取订单）',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '店铺要求：限早餐品类',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '用户类型：新注册用户',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '使用方式：移动端',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                                    child: Text(
                                      '活动日期：3个月',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    child: Text(
                                      '报名日期：长期',
                                      style: defaultStyle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.topLeft,
                                    margin: const EdgeInsets.only(top: Sizes.default10),
                                    child: Text(
                                      '报名费用：￥5.00',
                                      style: defaultStyle,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: Sizes.default60,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RouteGet.marketActivityInfo, arguments: false);
                            },
                            child: Text('活动详情', style: style)),
                        TextButton(
                            onPressed: () {
                              Get.toNamed(RouteGet.marketActivityPay);
                            },
                            child: Text('立即报名', style: style)),
                      ],
                    ),
                  )
                ],
              ),
            ),
            renderBottom(),
          ],
        ),
      ),
    );
  }

  Widget renderBottom() {
    if (noMore) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '- 已显示全部数据 -',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
    if (isLoading) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '努力加载中...',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.color797, fontSize: Sizes.default15),
            ),
            const Padding(padding: EdgeInsets.only(left: 10)),
            const SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(strokeWidth: 3),
            ),
          ],
        ),
      );
    } else {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        alignment: Alignment.center,
        child: Text(
          '上拉加载更多',
          style:
              StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15),
        ),
      );
    }
  }

  void loadMoreData() {
    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        noMore = true;
        isLoading = false;
      });
    });
  }

  void addActivity() async {
    final dialog = BrnDialog(
        verticalDivider: const VerticalDivider(
          width: 1.0,
          color: Colors.black,
        ),
        divider: const Divider(
          height: 1.0,
          color: Colors.black,
        ),
        contentWidget: ListBody(
          children: [
            Container(
              padding: const EdgeInsets.symmetric(vertical: Sizes.default15),
              alignment: Alignment.center,
              child: Text(
                '是否报名此活动?',
                style: StylesUtils.customTextStyle(
                    color: ColorsUtils.color000, fontSize: Sizes.default18),
              ),
            ),
          ],
        ),
        actionsWidget: [
          Container(
              alignment: Alignment.center,
              child: TextButton(
                onPressed: () {
                  Navigator.pop(context, 0);
                },
                child: Text(
                  '取消',
                  style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color333, fontSize: Sizes.default16),
                ),
              )),
          Container(
              alignment: Alignment.center,
              child: TextButton(
                onPressed: () {
                  Navigator.pop(context, 1);
                },
                child: Text('确定',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default16, color: ColorsUtils.colorFf6)),
              ))
        ],
        themeData: BrnDialogConfig(
            dialogWidth: 280,
            radius: Sizes.default10,
            titlePaddingSm: EdgeInsets.zero,
            titlePaddingLg: EdgeInsets.zero,
            contentPaddingSm: EdgeInsets.zero,
            contentPaddingLg: EdgeInsets.zero,
            contentTextAlign: TextAlign.center));
    int? result = await showDialog<int>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return dialog;
      },
    );
    if (result == 1) {
      /// 拨打电话
    }
  }
}
